package com.library.dao;

import com.library.bean.Lend;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class LendDao {

    @Resource
    private SqlSessionTemplate sqlSessionTemplate;

    private final static String NAMESPACE = "com.library.dao.LendDao.";
    public int lendBook(long bookId, long readId) {
        Map<String,Object> map=new HashMap<>();
        map.put("bookId",bookId);
        map.put("readerId",readId);

        return sqlSessionTemplate.update(NAMESPACE+"lendBook",map);
    }

    public int lendBooktwo(long bookId) {
        return sqlSessionTemplate.update(NAMESPACE+"lendBooktwo",bookId);
    }

    public int returnBook(long bookId, long readId) {
        Map<String,Object> map=new HashMap<>();
        map.put("bookId",bookId);
        map.put("readerId",readId);

        return sqlSessionTemplate.update(NAMESPACE+"returnBook",map);
    }

    public int returnBooktwo(long bookId) {
        return sqlSessionTemplate.update(NAMESPACE+"returnBooktwo",bookId);
    }

    public ArrayList<Lend> lendlist() {

        List<Lend> list=sqlSessionTemplate.selectList(NAMESPACE+"lendlist");
        return (ArrayList<Lend>)list;

    }

    public ArrayList<Lend> myLendList(long reader_id) {
        List<Lend> list=sqlSessionTemplate.selectList(NAMESPACE+"myLendList",reader_id);
        return (ArrayList<Lend>)list;
    }
}
